﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo.Tree
{
    public partial class Controls_TreeLoad : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Demo.Data.MatrixData data = new Data.MatrixData("datuhe");
                var table = data.FetchDataTable(PetaPoco.Sql.Builder
                    .Append("select * from 基础_物料分类 order by 排序号"));

                this.tree2.BindDataTable(table, "物料分类ID", "物料分类名称", "父物料分类ID", (row, node)=>{
                    node.text = "<font color='red'>" + node.text + "</font>";
                });
            }
        }

        protected void tree3_LoadChildren(object sender, ExtJSExtender.Controls.LoadChildrenEventArgs e)
        {
            Demo.Data.MatrixData data = new Data.MatrixData("datuhe");
            var dataTable = data.FetchDataTable(PetaPoco.Sql.Builder
                .Append("select * from 基础_物料分类")
                .Where("父物料分类ID=@0", e.NodeId)
                .OrderBy("排序号")
                );

            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    var tmpNode = new ExtJSExtender.Controls.TreeNode()
                    {
                        id = row["物料分类ID"].ToString(),
                        text = row["物料分类名称"].ToString(),
                        expanded = false
                    };
                    if (row["是否叶节点"].ToString() == "是")
                        tmpNode.leaf = true;

                    e.Node.children.Add(tmpNode);
                }
            }

        }
    }
}